package com.yijianonghu.controller.wxwebsite.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yijianonghu.service.wxwebsite.products.service.ServiceDetailService;
import com.yijianonghu.util.Constants;
import com.yijianonghu.util.ControllerUtils;
import com.yijianonghu.util.exception.NoResultReturnedException;
import com.yijianonghu.util.exceptionhandler.ControllerExceptionHandler;
import com.yijianonghu.util.result.MessageCode;
import com.yijianonghu.util.result.Result;

@Controller
@RequestMapping(value=Constants.WXConstants.WX_PREFIX+"service/", method=RequestMethod.POST)
public class ServiceController {
	
	private static final Logger logger=LoggerFactory.getLogger(ServiceController.class);
	
	@Autowired
	private ServiceDetailService serviceDetailService;
	
	@Autowired
	private ControllerExceptionHandler exceptionHandler;
	
	@RequestMapping(value="getServiceDetail")
	public @ResponseBody Result getServiceDetail(HttpSession session, Integer productId) throws InstantiationException{
		Result result=new Result();		
		result.setSuccess(false);
		List<Map<String, Object>> list=null;
		try {
			Integer userId=ControllerUtils.getUserId(session);
			list = serviceDetailService.getDetailInfo(userId, productId);
			if(list==null || list.isEmpty()){
				throw new NoResultReturnedException("服务详情为空！");
			}
		} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | IllegalAccessException
				| IllegalArgumentException | InvocationTargetException e) {
			exceptionHandler.handleException(logger, "获取服务详情失败！", result, MessageCode.SYSTEM_BUSY, e);
		} catch (NoResultReturnedException e) {
			exceptionHandler.handleException(logger, "服务详情为空！", result, MessageCode.WX_NO_RESULT_RETURNED, e);
		} catch (Exception e) {
			exceptionHandler.handleException(logger, "获取服务详情失败！解析数字失败！", result, MessageCode.SYSTEM_BUSY, e);
		}
		if(list!=null && (!list.isEmpty())){
			result.setInfor(list);
			result.setSuccess(true);
		}
		return result;
	}
}
